//  Gmof - A meta-object facility library based on OMG's EMOF 
//
//  Copyright (C) 2007 Peter Hagg <peter.hagg@iki.fi>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
#include <cppunit/TestAssert.h>
#include <cppunit/Test.h>
#include <cppunit/TestSuite.h>
#include <cppunit/TestCaller.h>
#include <cppunit/extensions/HelperMacros.h>

#include "../src/gmof.h"

class ReflectiveCollectionTest : public CppUnit::TestFixture 
{
	CPPUNIT_TEST_SUITE(ReflectiveCollectionTest);

	CPPUNIT_TEST(test_creation);
	CPPUNIT_TEST(test_add);
	CPPUNIT_TEST(test_add_all);
	CPPUNIT_TEST(test_remove);
	CPPUNIT_TEST(test_clear);
	CPPUNIT_TEST(test_add_twice);

	CPPUNIT_TEST_SUITE_END();

private:
	Gmof::ReflectiveCollection* collection;
	Gmof::Comment* comment;

public:
	void setUp()
	{
		collection = new Gmof::ReflectiveCollection();
		comment = new Gmof::Comment("Test comment");
	}

	void tearDown()
	{
		collection->unreference();
	}

	void test_creation()
	{
		CPPUNIT_ASSERT(collection);
		
		comment->unreference(); // Prevent memleak
	}
	
	void test_add()
	{
		CPPUNIT_ASSERT(collection->add(comment) == true);
		
		CPPUNIT_ASSERT(collection->size() == 1);
	}
	
	void test_add_all()
	{
		Gmof::ReflectiveSequence* list = new Gmof::ReflectiveSequence();
		list->add(0, new Gmof::Comment("Additional comment 1"));
		list->add(1, new Gmof::Comment("Additional comment 2"));
		
		collection->add(comment);
		CPPUNIT_ASSERT(collection->add_all(list) == true);
		
		// We can now unreference the sequence as the contents of it has 
		// been referenced in the collection
		list->unreference();
		
		CPPUNIT_ASSERT(collection->size() == 3);
	}
	
	void test_remove()
	{
		collection->add(comment);
		CPPUNIT_ASSERT(collection->remove(comment) == true);
		
		CPPUNIT_ASSERT(collection->size() == 0);
	}
	
	void test_clear()
	{
		collection->add(comment);
		collection->clear();
		
		CPPUNIT_ASSERT(collection->size() == 0);
	}
	
	void test_add_twice()
	{
		collection->add(comment);
		
		try
		{
			// This call should result in a Gmof::IllegalArgumenException being
			//  thrown.
			CPPUNIT_ASSERT(collection->add(comment) == false);
			CPPUNIT_FAIL("Expected exception didn't occur!");
		}
		catch (Gmof::IllegalArgumentException e)
		{
			CPPUNIT_ASSERT(e.code() == 0);
		}
	}
};

//CPPUNIT_TEST_SUITE_REGISTRATION(ReflectiveCollectionTest);

// End of file
